﻿using UnityEngine;
using System.Collections.Generic;
using UnityEngine.Serialization;

namespace ProjectTW.Foliage
{
    [ExecuteInEditMode]
    public class FoliageReceiver : ThreadItem
    {
        public static readonly List<FoliageReceiver> FReceivers = new List<FoliageReceiver>();

        // 9宫格Chunk列表
        [System.NonSerialized]
        private FoliageChunk[] m_ChunkNeighbors;
        // 草检查距离
        [SerializeField]
        protected float m_FoliageCheckDistance = 20f;

        private Vector3 m_LastCheckedPosition;
        private bool m_WasPositionChecked = false;

        [SerializeField]
        private Camera m_PlayerCamera;

        // 渲染队列接收者
        [System.NonSerialized]
        private RenderingQueueReceiver m_QueueReceiver;

        // 是否渲染草
        public bool IsFoliageReceiver = true;

        public FoliageChunk[] ChunkNeighbors
        {
            get
            {
                if (m_ChunkNeighbors == null)
                {
                    m_ChunkNeighbors = new FoliageChunk[9];
                    m_ChunkNeighbors = FoliageStandaloneUtility.GetFoliageChunksNeighbors(transform.position - FoliageManager.Instance.transform.position, m_ChunkNeighbors);
                }

                return m_ChunkNeighbors;
            }
        }

        /// <summary>
        /// 可见区域中间的Chunk
        /// </summary>
        public FoliageChunk MiddleFoliageChunkFromNeighbors
        {
            get
            {
                return ChunkNeighbors[4];
            }
        }

        public float FoliageCheckDistance
        {
            get
            {
                return m_FoliageCheckDistance;
            }
            set
            {
                m_FoliageCheckDistance = value;
            }
        }

        public Camera PlayerCamera
        {
            get
            {
                if (m_PlayerCamera == null)
                {
                    m_PlayerCamera = GetComponentInChildren<Camera>();
                }

                return m_PlayerCamera;
            }
            set
            {
                m_PlayerCamera = value;
            }
        }

        public RenderingQueueReceiver QueueReceiver
        {
            get
            {
                if (m_QueueReceiver == null)
                {
                    m_QueueReceiver = new RenderingQueueReceiver();
                    m_QueueReceiver.Transform = transform;
                }

                return m_QueueReceiver;
            }
            internal set
            {
                m_QueueReceiver = value;
            }
        }

        protected override void OnEnable()
        {
            base.OnEnable();
            FReceivers.Add(this);
        }

        protected override void OnDisable()
        {
            base.OnDisable();
            FReceivers.Remove(this);
        }

        protected override void Update()
        {
            if (!Application.isPlaying)
            {
                return;
            }

            base.Update();

            if (IsFoliageReceiver && FoliageManager.Instance != null && FoliageManager.Instance.Enabled)
            {
                // 检查释放需要更新
                if ((!m_WasPositionChecked || Vector3.Distance(m_LastCheckedPosition, transform.position) >= FoliageCheckDistance))
                {
                    m_ChunkNeighbors = FoliageStandaloneUtility.GetFoliageChunksNeighbors(transform.position - FoliageManager.Instance.transform.position, m_ChunkNeighbors);

                    m_WasPositionChecked = true;
                    m_LastCheckedPosition = transform.position;
                }

                // 检查位置更新
                QueueReceiver.Camera = PlayerCamera;
                QueueReceiver.CheckPositionChange();

                // 执行渲染
                RenderingPipielineCore.RenderQueue(QueueReceiver, PlayerCamera);
            }
        }
    }
}
